﻿using System;
using DosTexPrj.Core.Models;
using DosTexPrj.Implement.NHibernate;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace DosTexPrj.CreateDatabase
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                BootTrapper.BootAll();
                Configuration config = MyServiceLocator.DoGetInstance<Configuration>();
                ISessionFactory sessionFactory = MyServiceLocator.DoGetInstance<ISessionFactory>();
                ISession session = sessionFactory.OpenSession();
                SchemaExport export = new SchemaExport(config);
                try
                {
                    export.Drop(false, true);
                }
                catch { };
                export.Create(false, true);
                export.Execute(false, true, false, session.Connection, null);
                Console.WriteLine("Create database successful.");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Init some data");
                session.Save(new FabricType { FabricTypeName = "Dummy FabricType" });
                session.Save(new OrderStatus { OrderStatusName = "Dummy Order Status" });
                session.Save(new Composition { CompostionName = "Dummy Composition" });
                session.Save(new DeliveryMethod { DeliveryMethodName = "Dummy Delivery Method" });
                session.Save(new PaymentMethod { PaymentMethodName = "Dummy Payment Method" });
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Create database fail: {0}", ex));
            }
            Console.WriteLine("Press any key to close.");
            Console.ReadLine();
        }
    }
}